<?php
namespace wlwx;
/**
 * Created by PhpStorm.
 * User: zhouzhao
 * Date: 2017年4月10日 17:55:23
 */

class SmsOperator {
    public function __construct() {

    }

    /**
     * 发送普通短信
     * @param array $data
     * @return Result
     */
    public function send_comSms($data = array()) {
        list($t1, $t2) = explode(' ', microtime()); 
        $timestamp = (float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000);
        $sign = MD5(config('conf_sms.ACCOUNT_SID').config('conf_sms.AUTH_TOKEN').$timestamp);
        $paramUrl = 'accountSid='.config('conf_sms.ACCOUNT_SID').'&to='.$data['destMobiles'].'&templateid='.$data['templateid']."&param=".$data['code']."&timestamp=".$timestamp.'&sig='.$sign.'&smsType=100000';
        return $this->post_curl(config('conf_sms.SEND_SMS_URL'), $paramUrl);
    }

    public function post_curl($url, $data)
    {
        //初始化
        $curl = curl_init();
        //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);
        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, 1);
        //设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //设置post方式提交
        curl_setopt($curl, CURLOPT_POST, 1);

        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        //执行命令
        $data = curl_exec($curl);
        //关闭URL请求
        curl_close($curl);
        //显示获得的数据
        return json_decode($data, true);
    }

}

